package com.zhku.onlineshop.Dao;


import com.zhku.onlineshop.Pojo.Order;
import com.zhku.onlineshop.Pojo.OrderItem;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface OrderDao {
    @Insert("INSERT INTO orders (customer_id, total_price, status,address_id) VALUES (#{customer_id}, #{total_price}, #{status},#{address})")
    @Options(useGeneratedKeys = true, keyProperty = "id") // 获取生成的订单ID
    boolean insertOrder(Order order);

    @Update("UPDATE orders SET status = #{status} WHERE id = #{id}")
    boolean updateOrderStatus(@Param("id") Integer id, @Param("status") String status);

    @Delete("DELETE FROM orders WHERE id = #{id}")
    boolean deleteOrder(Integer id);

    @Select("SELECT * FROM orders WHERE customer_id = #{customer_id}")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "customer_id", column = "customer_id"),
            @Result(property = "total_price", column = "total_price"),
            @Result(property = "status", column = "status"),
            @Result(property = "address",column = "address_id"),
            @Result(property = "order_items", column = "id",
                    many = @Many(select = "getOrderItemsByOrderId"))
    })
    List<Order> findOrdersByCustomerId(Integer customer_id);

    @Select("SELECT id,order_id ,product_id, quantity, price FROM order_items WHERE order_id = #{order_id}")
    List<OrderItem> getOrderItemsByOrderId(@Param("order_id") Integer order_id);

    @Select("SELECT * FROM orders WHERE status = #{status};")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "customer_id", column = "customer_id"),
            @Result(property = "total_price", column = "total_price"),
            @Result(property = "status", column = "status"),
            @Result(property = "address",column = "address_id"),
            @Result(property = "order_items", column = "id",
                    many = @Many(select = "getOrderItemsByOrderId"))
    })
    List<Order> getOrderBystatus(@Param("status") Integer status);

}
